package com.hdf.etdemo.image;

import java.util.ArrayList;

import com.hdf.easytools.base.EasyToolsActivity;
import com.hdf.etdemo.R;
import com.hdf.etdemo.R.id;
import com.hdf.etdemo.R.layout;
import com.hdf.etdemo.image.select.MultiImageSelectorActivity;
import com.hdf.etdemo.image.select.adapter.PubSelectedImgsAdapter;
import com.hdf.etdemo.image.select.adapter.PubSelectedImgsAdapter.OnItemClickClass;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;

public class ImageSelectActivity extends EasyToolsActivity {

	private static final int REQUEST_IMAGE = 2;

	private ArrayList<String> mSelectPath;

	GridView gridView;
	PubSelectedImgsAdapter pubSelectedImgsAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_select);
		/** 设置标题显示 */
		this.TitleVisib(0, 1, 1, 0, 0);
		/** 绑定标题点击事件 */
		this.TitleClick(0, 1, 0, 0, 0);
		/** 设置标题样式ʽ */
		this.TitleStyle(null, R.drawable.et_back, "多图选择", null, 0);
		gridView = (GridView) findViewById(R.id.gridView);
		findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(ImageSelectActivity.this, MultiImageSelectorActivity.class);
				// 是否显示拍摄图片
				intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
				// 最大可选择图片数量
				intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
				// 选择模式
				intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
				// 默认选择
				if (mSelectPath != null && mSelectPath.size() > 0) {
					intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
				}
				startActivityForResult(intent, REQUEST_IMAGE);

			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_IMAGE) {
			if (resultCode == RESULT_OK) {
				mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
				pubSelectedImgsAdapter = new PubSelectedImgsAdapter(getApplicationContext(), mSelectPath,
						new OnItemClickClass() {
							@Override
							public void OnItemClick(View v, String filepath) {
								mSelectPath.remove(filepath);
								pubSelectedImgsAdapter.notifyDataSetChanged();
							}
						});
			}
			gridView.setAdapter(pubSelectedImgsAdapter);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		default:
			break;
		}
	}

}
